package com.lhy.controller;


import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.lhy.domain.NewMessage;
import com.lhy.domain.Result;
import com.lhy.service.IUserService;



@Controller
@RequestMapping("/createNewMessage")
public class CreateNewMessageController {
	@Autowired
	private IUserService userService;
	@RequestMapping("")
	public String toIndex(HttpServletRequest request, Model model,String newMessage_id){
		return "createnewmessage";
	}
	@RequestMapping("/update")
	public ModelAndView index(String newMessage_id){
		ModelAndView mv = new ModelAndView();
		if(!StringUtils.isEmpty(newMessage_id)){
			NewMessage message = userService.queryNewMessageById(Integer.valueOf(newMessage_id));
			mv.addObject("message",message);
		}
		mv.setViewName("createnewmessage");
		return mv;
	}
	@RequestMapping("/do")
	public @ResponseBody
	Map<String, Object> add(HttpSession session,String author, String title, String content,String messageType)  {
		Map<String, Object> returnMap = new HashMap<>();
		Result result=new Result();
		result.setCode("1");
		result.setDes("成功");
		Date date = new Date();       
		Timestamp create_dt = new Timestamp(date.getTime());
		try{
			Integer sequence = Integer.valueOf(session.getAttribute("usersequence").toString());
			userService.insertNewMessageByClob(author,title,content,create_dt,sequence,Integer.valueOf(messageType));
		}catch(Exception e){
			e.printStackTrace();
			result.setCode("0");
			result.setDes(e.getMessage());
		}
		returnMap.put("result", result);
		return returnMap;
	}
}
